<template>
	<jk-dialog
		v-model="showDialog"
		:title="`${dataId ? '编辑' : '新增'}-工序`"
		width="600px"
		:show-content-loading="showLoading"
		:confirm-btn-loading="saveBtnLoading"
		@on-cancel="onVisibleChange(false)"
		@on-visible-change="onVisibleChange"
		@on-confirm="onConfirmBtn"
	>
		<el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px" :show-message="false">
			<el-row ref="box1">
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="编码" prop="code">
						<el-input v-model="ruleForm.code" placeholder="编码" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="名称" prop="name">
						<el-input v-model="ruleForm.name" placeholder="名称" />
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="区域" prop="workshopId">
						<jk-select v-model="ruleForm.workshopId" size="medium" :clearable="false" :option-list="workshopList" placeholder="区域"></jk-select>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="是否报工">
						<el-checkbox v-model="ruleForm.ifReport" size="medium">是</el-checkbox>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="是否排产">
						<el-checkbox v-model="ruleForm.ifScheduling" size="medium">是</el-checkbox>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="看板是否展示">
						<el-checkbox v-model="ruleForm.ifShow" size="medium">是</el-checkbox>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="是否统计能耗">
						<el-checkbox v-model="ruleForm.ifLookElectricity" size="medium">是</el-checkbox>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item class="margin-bottom-10" label="排序" prop="sortNum">
						<jk-number-input v-model="ruleForm.sortNum" placeholder="排序" />
					</el-form-item>
				</el-col>
			</el-row>
			<jk-divider-card title="工资基准" placement="center" class="margin-top-20">
				<div ref="box2">
					<el-form-item class="margin-bottom-10" label="单价">
						<div class="flex-start-center">
							<jk-number-input v-model="ruleForm.unitPrice" placeholder="单价" />
							<!--<jk-number-input v-model="ruleForm.referenceOutput" placeholder="基准产量(M)" />-->
							<!--<jk-number-input v-model="ruleForm.regularSalary" :placeholder="['BT', 'TB', 'EB', 'MB'].includes(ruleForm.code.toUpperCase()) ? '单价' : '固定工资'" />-->
						</div>
					</el-form-item>
				</div>
				<!--<el-form-item class="margin-bottom-10" label="满勤补贴标准" prop="fullAttendanceSubsidy">
					<jk-number-input v-model="ruleForm.fullAttendanceSubsidy" placeholder="满勤补贴金额" class="margin-bottom-10" />
					<jk-table border :data="ruleForm.fullAttendanceSubsidyConfigList || []" :max-height="dialogTableHeight - 160">
						<vxe-column title="操作" align="center" field="groupName" :width="70">
							<template slot="header">
								<jk-button type="text" size="mini" icon="el-icon-plus" @click="onAddBtn">添加</jk-button>
							</template>
							<template slot-scope="scope">
								<jk-button size="mini" icon="el-icon-delete" @click="ruleForm.fullAttendanceSubsidyConfigList.splice(scope.rowIndex, 1)" />
							</template>
						</vxe-column>
						<vxe-column title="缺勤天数" align="right" field="minDay" :min-width="180">
							<template slot-scope="scope">
								<div class="flex-start-center">
									<jk-number-input v-model="scope.row.minDay" size="small" placeholder="最小天数" />
									<span>~</span>
									<jk-number-input v-model="scope.row.maxDay" size="small" placeholder="最大天数" />
								</div>
							</template>
						</vxe-column>
						<vxe-column title="扣减金额" align="right" field="deductionAmount" :width="120">
							<template slot-scope="scope">
								<jk-number-input v-model="scope.row.deductionAmount" size="small" placeholder="扣减金额" />
							</template>
						</vxe-column>
					</jk-table>
				</el-form-item>-->
			</jk-divider-card>
		</el-form>
	</jk-dialog>
</template>

<script>
    import calHeight from '/src/mixins/cal-dialog-height';
    import { spinProcessCreate, spinProcessUpdate, spinProcessGet } from '../../../api/technology/process';
    export default {
        name: 'Process',
        props: {
            dialogState: {
                type: Boolean,
                default: false
            },
            dataId: {
                type: [String, Number],
                default: ''
            }
        },
        components: {
        },
        mixins: [calHeight],
        data() {
            return {
                saveBtnLoading: false,
                showDialog: false,
                showLoading: true,
                confirmBtnLoading: false,
                saveDialogState: false,
                ruleForm: {
                    'code': '',
                    'workshopId': '',
                    'ratio': null,
                    'ifBottleneck': true,
                    'ifPlan': true,
                    'ifReport': true,
                    'ifScheduling': true,
                    'ifStock': true,
                    'name': '',
                    'ifLookElectricity': true,
                    'ifShow': true,
                    'unitPrice': null,
                    'referenceOutput': null,
                    'regularSalary': null,
                    'sortNum': 1,
                    'typeId': 0,
                    'typeName': '',
                    'fullAttendanceSubsidy': 400,
                    'fullAttendanceSubsidyConfigList': []
                },
                workshopList: [],
                rules: {
                    typeId: [{ required: true, trigger: 'change' }],
                    code: [{ required: true, trigger: 'change' }],
                    workshopId: [{ required: true, trigger: 'change' }],
                    name: [{ required: true, trigger: 'change' }],
                    status: [{ required: true, trigger: 'change' }]
                }
            };
        },
        methods: {
            onAddBtn() {
                this.ruleForm.fullAttendanceSubsidyConfigList.push({
                    minDay: null,
                    maxDay: null,
                    deductionAmount: null
                });
            },
            getParamData() {
                const params = JSON.parse(JSON.stringify(this.ruleForm));
                if (!this.dataId) {
                    delete params.id;
                }
                params.workshopCode = this.workshopList.find(x => x.id === params.workshopId).code;
                params.workshopName = this.workshopList.find(x => x.id === params.workshopId).name;
                return params;
            },
            onConfirmBtn() {
                this.$refs['ruleForm'].validate(valid => {
                    if (valid) {
                        this.createAndUpdateRequest();
                    } else {
                        this.$tipsBar(this, 'validator');
                    }
                });
            },
            createAndUpdateRequest() {
                this.saveBtnLoading = true;
                const api = !this.dataId ? spinProcessCreate : spinProcessUpdate;
                api(this.getParamData()).then(res => {
                    console.log('aaaaaaaaa', this.getParamData());
                    this.saveBtnLoading = false;
                    if (res.data.status === 200) {
                        this.$tipsBar(this, 'save');
                        this.$emit('on-finish');
                    }
                });
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                    this.$nextTick(() => {
                        this.$refs.ruleForm.resetFields();
                    });
                }
            },
            getDetailRequest() {
                return spinProcessGet({ id: this.dataId }).then(res => {
                    if (res.data.status === 200) {
                        const resData = res.data.res || {};
                        resData.fullAttendanceSubsidyConfigList = resData.fullAttendanceSubsidyConfigList || [];
                        this.ruleForm = resData;
                    }
                });
            },
            async getDependentData() {
                this.showLoading = true;
                if (!this.ruleForm.workshopId) {
                    this.$set(this.ruleForm, 'workshopId', this.$store.state.user.userWorkshop.defaultWorkshopId);
                }
                this.workshopList = this.$store.state.user.userWorkshop.workshopList;
                if (this.dataId) await this.getDetailRequest();
                this.showLoading = false;
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.getDependentData();
                }
            }
        }
    };
</script>
